package com.csust.reflection.class_;

import com.csust.Car;

import java.lang.reflect.Field;

public class Class02 {
    public static void main(String[] args) throws Exception {
        String classFullPath = "com.csust.Car";
        Class clazz = Class.forName(classFullPath);

        //1.输出clazz
        System.out.println(clazz);//class com.csust.Car
        System.out.println(clazz.getClass());//class java.lang.Class
        //2.得到包名
        System.out.println(clazz.getPackage().getName());//com.csust
        //3.得到全类名
        System.out.println(clazz.getName());//com.csust.Car
        //4.通过clazz创建一个对象(实例)
        Car c = (Car) clazz.newInstance();
        System.out.println(c);
        //5.获取实例得属性
        System.out.println("===============");
        Field cbrand = clazz.getField("brand");
        System.out.println(cbrand.get(c));
        //6.通过反射给属性赋值
        cbrand.set(c,"比亚迪-汉");
        System.out.println(cbrand.get(c));
        //7. 获得所有的属性，并遍历属性
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field.get(c));
        }


    }
}
